package org.dreamwork.jasmine2.sample.page;

import org.dreamwork.jasmine2.events.EventException;
import org.dreamwork.jasmine2.web.controls.Page;
import org.dreamwork.util.StringUtil;
import sample.data.DataItem;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: seth.yang
 * Date: 2010-10-12
 * Time: 10:45:00
 */
public class PDFPage extends Page {
    protected List<DataItem> data;

    private static String[] descs = {
        "新浪科技讯 ",
        "北京时间10月7日下午消息，",
        "据国外媒体报道，",
        "AOL首席技术官亚历山大·",
        "古纳里斯(Alexander Gounares)周三表示，",
        "该公司有可能收购更多网络媒体，",
        "甚至传统报纸，",
        "借此提升数字内容业务"
    };

    public PDFPage () {
        data = new ArrayList<DataItem> (10);
        for (int i = 0; i < 10; i ++) {
            data.add (new DataItem (String.format ("Item %d", i + 1), descs [i % descs.length]));
        }
    }

    @Override
    public void onPageLoadCompleted (Page page) throws EventException {
        String media = request.getParameter ("media");
        if (!StringUtil.isEmpty (media)) {
            if ("html".equalsIgnoreCase (media)) this.contentType = "text/html;charset=utf-8";
            else if ("pdf".equalsIgnoreCase (media)) contentType = "application/pdf";
        }
        try {
            dataBind ();
        } catch (Throwable throwable) {
            throwable.printStackTrace ();
        }
    }
}